#!/usr/bin/env python
# encoding: utf-8
"""
calibration_circle.py

Created by William Patera on 2011-09-08.
Copyright (c) 2011 William Patera. All rights reserved.
"""
import numpy as np
from OpenGL.GL import *
import pygame
from pygame.locals import *

class CalibrationCircle():
	def __init__(self, radius=20, x=0.0, y=0.0, steps=5, screen=(640,480), verbose=True):
		self.radius = radius
		self.x = x
		self.y = y
		self.steps = steps
		self.screen = screen
		self.verbose = verbose
	
	def update(self, new_x, new_y):
		# update on event
		self.x = new_x
		self.y = new_y	
		if self.verbose: 
			print self._distance(0.5*self.screen[0], 0.5*self.screen[1], self.x, self.y)
		
	def update_rand_pos(self, screen):
		#random update position within screen size
		new_x = np.random.randint(0, high=screen[0])
		new_y = np.random.randint(0, high=screen[1])
		dist = self._distance(self.x, self.y, new_x, new_y)
		self.update(new_x, new_y)

	def _distance(self, x1, y1, x2, y2):
		dx = x2 - x1
		dy = y2 - y1
		dsquared = dx**2 + dy**2
		result = np.sqrt(dsquared)
		return result
		
	def _circle(self, radius, steps):
		glLineWidth(0.5)
		glBegin(GL_LINE_STRIP)
		for a in range(0,360,steps):
			glVertex2f(self.x + (np.sin(a) * radius), self.y + (np.cos(a) * radius))
		glEnd()
	
	def draw_circle(self):
		glOrtho(0, self.screen[0], self.screen[1], 0, -1, 1)
		glMatrixMode(GL_MODELVIEW)
		glLoadIdentity()
		glPushMatrix()
		glColor4f(1.0,0.0,0.0,0.5)
		#glTranslatef(self.x, self.y, 0.0)
		self._circle(self.radius, self.steps)
		glColor4f(0.0, 1.0, 0.5, 0.5)
		self._circle(0.3*self.radius, self.steps)
		glPopMatrix()
	
	def get_pos(self):
		return self.x, self.y

